$(() => {
    findById(id);
    //渲染出生日期
    layui.use(function () {
        const laydate = layui.laydate;
        laydate.render({
            elem: "#birthday",
            type: "date"
        });
    });
});

//查询指定id的学生信息
function findById(id) {
    $.ajax({
        url: ctx + "/admin/classes/id",
        method: "get",
        data: {
            id
        },
        success(resp) {
            const cla = resp.classes;
            $("#id").val(cla.id);
            $("#class_name").val(cla.class_name);
            $("#begin_time").val(cla.begin_time);
            $("#end_time").val(cla.end_time);
            $("#major_id").val(cla.major_id);
            $("#student_cnt").val(cla.student_cnt);
            $("#status").val(cla.status);
            $("#description").val(cla.description);
        }
    });
}

//修改
function submit(cb = $.noop) {
    let class_name = $("#class_name").val();
    let begin_time = $("#begin_time").val();
    let end_time = $("#end_time").val();
    let major_id = $("#major_id").val();
    let student_cnt = $("#student_cnt").val();
    let status = $("#status").val();
    let description = $("#description").val();


    //前端校验：js校验
    if (class_name.trim() === "") {
        layer.msg("名称不可为空");
        return;
    }


    $.ajax({
        url: ctx + "/admin/classes/edit",
        method: "post",
        data: {
            id,
            class_name,
            begin_time,
            end_time,
            major_id,
            student_cnt,
            status,
            description
        },
        success(resp) {
            cb(resp.success);
        }
    });


}